package com.jic.order.utils;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Service
public class WxPayConfiguration implements WXPayConfig {

    private byte[] certData;

    private final String appId = "wxb9eaa8752b647f28";

    private final String mchId = "1603438327";

    private final String mchKey = "hn116c7b3a76541a0d6e97e4bd5994d2";

    private final String keyPath = "/data/config/chidori/apiclient_cert.p12";

    public WxPayConfiguration() throws Exception { // 构造方法读取证书, 通过getCertStream 可以使sdk获取到证书


        /**
         * 此处try-catch 住了，本地启动会报错，需要问下王军；
         * 缺少apiclient_cert.p12文件；
         * tzc：2020-11-5 21:00:22
         */
        try {
            File file = new File(keyPath);
            InputStream certStream = new FileInputStream(file);
            this.certData = new byte[(int) file.length()];
            certStream.read(this.certData);
            certStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return mchKey;
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

}
